package com.controller.sys;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.Result;
import com.common.log.OperationLog;
import com.common.log.OperationType;
import com.entity.sys.Role;
import com.entity.sys.UserRole;
import com.service.sys.RoleService;
import com.service.sys.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author clay
 * @since 2022-02-15
 */
@Api("RoleController")
@RestController
@RequestMapping("/sys/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private UserRoleService userRoleService;

    @ApiOperation("通过id获取权限")
    @GetMapping("info/{id}")
    public Result info(@PathVariable String id) {
        return Result.success(roleService.getById(id));
    }

    @ApiOperation("添加权限")
    @PostMapping("/add")
    @OperationLog(operationType = OperationType.INSERT, describe = "添加权限信息", entity = Role.class, primaryKey = "id")
    public Result add(@RequestBody Role role) {
        if (roleService.save(role)) {
            return Result.success("操作成功");
        }
        return Result.fail("添加失败!");
    }

    @ApiOperation("修改权限")
    @OperationLog(operationType = OperationType.UPDATE, describe = "修改权限信息", entity = Role.class, primaryKey = "id")
    @PostMapping("/update")
    public Result update(@RequestBody Role role) {
        if (roleService.updateById(role)) {
            return Result.success("操作成功");
        }
        return Result.fail("修改失败!");
    }

    @ApiOperation("删除权限")
    @OperationLog(operationType = OperationType.DELETE, describe = "删除权限信息", entity = Role.class, primaryKey = "id")
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        if (roleService.deleteById(id)) {
            return Result.success("操作成功");
        }
        return Result.fail("删除失败!");
    }

    @ApiOperation("获取所有权限")
    @PostMapping("/select")
    public Result select(@RequestBody Role role) {
        System.out.println(JSON.toJSON(role));
        QueryWrapper wrapper = new QueryWrapper<Role>();
        if (!StringUtils.isEmpty(role.getName())) {
            wrapper.like("name", role.getName());
        }
        if (!StringUtils.isEmpty(role.getNameZh())) {
            wrapper.like("name_zh", role.getNameZh());
        }
        return Result.success(roleService.list(wrapper));
    }


    @ApiOperation("权限绑定菜单")
    @PostMapping(value = "/assignMenu/{id}")
    public Result assignMenu(@PathVariable String id, @RequestBody List<String> menuIds) {
        return Result.success(roleService.assignMenu(id, menuIds));
    }


    @ApiOperation("通过用户ID查询权限")
    @PostMapping(value = "/selectRole/{id}")
    public Result selectById(@PathVariable String id){
        List<UserRole> userRoles = userRoleService.list(new QueryWrapper<UserRole>().select().eq("user_id", id));
        return Result.success(userRoles);
    }
}
